package com.kuaimacode.kframework.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.vo.request.FeedbackReq;
import com.kuaimacode.kframework.mybatis.mapper.index.FeedbackMapper;
import com.kuaimacode.kframework.mybatis.models.index.Feedback;
import com.kuaimacode.kframework.mybatis.models.user.OrderSimple;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FeedbackService extends ServiceImpl<FeedbackMapper, Feedback> {

    /**
     * 查询用户投诉列表
     * @return
     */
    public List<Feedback> queryUserFeedbackList(Long userId){

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return baseMapper.selectList(queryWrapper);
    }


    /**
     *  分页查找所有的数据
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @return
     */
    public PageInfo<Feedback> queryUserFeedbackListPage(int pageNum, int pageSize,Long userId,String type) {
        PageHelper.startPage(pageNum, pageSize,true,false,null);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("type",type);
        List<Feedback> datas = baseMapper.selectList(queryWrapper);
        return new PageInfo<Feedback>(datas);
    }

    /**
     * 保存
     * @param feedbackReq
     * @param userId
     * @return
     */
    public Feedback saveFeedback(FeedbackReq feedbackReq,Long userId){

        Feedback feedback = new Feedback();
        Map map = baseMapper.getMaxSeqNo();
        feedback.setSeqNo((String)map.get("id"));
        feedback.setContact(feedbackReq.getContact());
        feedback.setContactWay(feedbackReq.getContactWay());
        feedback.setContent(feedbackReq.getContent());
        feedback.setSubType(feedbackReq.getSubType());
        feedback.setType(feedbackReq.getType());
        feedback.setOrderId(feedbackReq.getOrderId());
        feedback.setImgs(feedbackReq.getImgs());
        feedback.setUserId(userId);
        feedback.setCreateAt(new Date());
        feedback.setState(1);
        baseMapper.insert(feedback);

        return feedback;

    }


    /**
     * 获取投诉反馈详细信息
     * @param id
     * @return
     */
    public Feedback getById(String id){
        QueryWrapper<Feedback> qryWrapper = new QueryWrapper<>();
        qryWrapper.and(wrapper -> wrapper.eq("id", id).or().eq("seq_no", id));

        return baseMapper.selectOne(qryWrapper);
    }

    /**
     *  分页查找所有的数据
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @param type  1-投诉 2-反馈
     * @return
     */
    public PageInfo<OrderSimple> queryOdersByUserId(Long userId, Integer type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize,true,false,null);
        List<OrderSimple> datas = baseMapper.queryOdersByUserId(userId, type);
        return new PageInfo<OrderSimple>(datas);
    }

    /**
     *  根据订单号查询是否已经评价
     * @return
     */
    public List<Feedback> findByOrderId(Long orderId) {
        QueryWrapper<Feedback> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("order_id", orderId);
        List<Feedback> datas = baseMapper.selectList(qryWrapper);
        return datas;
    }



}
